package com.astronomy.recruit.service;

import com.astronomy.entity.PageResult;
import com.astronomy.recruit.mapper.EnterpriseMapper;
import com.astronomy.recruit.pojo.Enterprise;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class EnterpriseService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    /**
     * 全部数据
     * @return
     */
    public List<Enterprise> findAll(){
        return enterpriseMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Enterprise findById(String id){
        return  enterpriseMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询热门企业
     * @param ishot
     * @return
     */
    public List<Enterprise> hotList(String ishot) {
        Enterprise enterprise=new Enterprise();
        enterprise.setIshot(ishot);
        return enterpriseMapper.select(enterprise);
    }
    /**
     * 增加
     * @param enterprise
     */
    public void add(Enterprise enterprise){
        enterpriseMapper.insert(enterprise);
    }

    /**
     * 修改
     * @param enterprise
     */
    public void update(Enterprise enterprise){
        enterpriseMapper.updateByPrimaryKey(enterprise);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id){
        enterpriseMapper.deleteByPrimaryKey(id);
    }


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    public Page<Enterprise> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<Enterprise>)enterpriseMapper.selectAll();
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<Enterprise> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return enterpriseMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Enterprise.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // ID
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
           	}
            // 企业名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
           	}
            // 企业简介
            if(searchMap.get("summary")!=null && !"".equals(searchMap.get("summary"))){
                criteria.andLike("summary","%"+searchMap.get("summary")+"%");
           	}
            // 企业地址
            if(searchMap.get("address")!=null && !"".equals(searchMap.get("address"))){
                criteria.andLike("address","%"+searchMap.get("address")+"%");
           	}
            // 标签列表
            if(searchMap.get("labels")!=null && !"".equals(searchMap.get("labels"))){
                criteria.andLike("labels","%"+searchMap.get("labels")+"%");
           	}
            // 坐标
            if(searchMap.get("coordinate")!=null && !"".equals(searchMap.get("coordinate"))){
                criteria.andLike("coordinate","%"+searchMap.get("coordinate")+"%");
           	}
            // 是否热门
            if(searchMap.get("ishot")!=null && !"".equals(searchMap.get("ishot"))){
                criteria.andLike("ishot","%"+searchMap.get("ishot")+"%");
           	}
            // LOGO
            if(searchMap.get("logo")!=null && !"".equals(searchMap.get("logo"))){
                criteria.andLike("logo","%"+searchMap.get("logo")+"%");
           	}
            // URL
            if(searchMap.get("url")!=null && !"".equals(searchMap.get("url"))){
                criteria.andLike("url","%"+searchMap.get("url")+"%");
           	}

            // 职位数
            if(searchMap.get("jobcount")!=null ){
                criteria.andEqualTo("jobcount",searchMap.get("jobcount"));
            }

        }
        return example;
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    public PageResult<Enterprise> findPage(Map<String, Object> searchMap, int page, int size){
        Example example = createExample(searchMap);
        PageHelper.startPage(page,size);
        List<Enterprise> enterprises = enterpriseMapper.selectByExample(example);
        PageInfo<Enterprise> enterprisePageInfo = new PageInfo<>(enterprises);
        return new PageResult<>(enterprisePageInfo.getTotal(),enterprisePageInfo.getList());
    }


}
